<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的QQ互联参数
$qqInterconnect = $_POST['qq_interconnect'] ?? '';

// 检查 QQ 互联参数是否为空
if (empty($qqInterconnect)) {
    $result = [
        'status' => 'error',
        'message' => 'QQ 互联参数不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询用户信息，支持通过 QQ互联 字段查询
    $loginStmt = $pdo->prepare("SELECT * FROM mi_users WHERE QQ互联 = :qq_interconnect");
    $loginStmt->bindParam(':qq_interconnect', $qqInterconnect);
    $loginStmt->execute();
    $user = $loginStmt->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        // 返回用户所有信息
        $result = [
            'status' => 'success',
            'message' => '获取用户信息成功',
            'user' => $user
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    } else {
        // 用户不存在
        $result = [
            'status' => 'error',
            'message' => '用户不存在'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>
